package com.hkyl.hospital.service.impl;

import com.github.pagehelper.PageHelper;
import com.hkyl.hospital.entity.Operation;
import com.hkyl.hospital.entity.OperationExample;
import com.hkyl.hospital.mapper.OperationMapper;
import com.hkyl.hospital.service.OperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sjh
 * @date 2023/07/10 19:52:39
 */
@Service
@Transactional
public class OperationServiceImpl implements OperationService {

    @Autowired
    private OperationMapper operationMapper;

    @Override
    public int insert(Operation operation) {
        return operationMapper.insert(operation);
    }

    @Override
    public int delete(Integer id) {
        return operationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(Operation operation) {
        return operationMapper.updateByPrimaryKeySelective(operation);
    }

    @Override
    public List<Operation> select(Integer pageNumber, Integer pageSize) {

        //首先查询全部的手术，防止分页的时候包括子手术
        List<Operation> operations = operationMapper.selectByExample(null);
        //在进行分页
        PageHelper.startPage(pageNumber, pageSize);
        //找到根手术
        OperationExample operationExample = new OperationExample();
        operationExample.or().andParentIdIsNull();
        List<Operation> root = operationMapper.selectByExample(operationExample);
        //然后跟手术进行孩子查询，进行遍历
        for (Operation operation : root) {
            getChildren(operations, operation);
        }
        return root;
    }

    @Override
    public List<Operation> search(String operationName,Integer pageNumber, Integer pageSize) {
        OperationExample operationExample = new OperationExample();
        operationExample.or().andOperationNameLike("%"+operationName+"%");
        PageHelper.startPage(pageNumber, pageSize);
        List<Operation> operations = operationMapper.selectByExample(operationExample);
        return operations;
    }

    public static void getChildren(List<Operation> operations, Operation parent) {
        for (Operation childrenDept : operations) {
            if (parent.getOperationId() == childrenDept.getParentId()) {
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(childrenDept);
                getChildren(operations, childrenDept);
            }
        }
    }

    @Override
    public List<Operation> listOperation() {
        List<Operation> operations = operationMapper.selectByExample(null);
        OperationExample operationExample = new OperationExample();
        operationExample.or().andParentIdIsNull();
        List<Operation> roots = new ArrayList<>();
        for (Operation operation : operations) {
            if (operation.getParentId() == null){
                getChildren(operations, operation);
                roots.add(operation);
            }
        }
        return roots;
    }
}
